package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"net/http"
	"note/service/conf"
	"note/service/handler"
	"note/service/logger"
	"note/service/routers"
	"xorm.io/core"
)

func init() {
	//初始化配置
	load := conf.FileLoad{
		Name: "conf",
		Path: "./conf",
	}
	conf.Setup(load.Load)
	//日志配置
	logger.Setup()

	engine, err := xorm.NewEngine(
		"mysql",
		fmt.Sprintf("%s:%s@%s/%s?charset=utf8", conf.Config.Db.User, conf.Config.Db.Pwd, "("+conf.Config.Db.Host+")", conf.Config.Db.Name),
	)
	if err != nil {
		fmt.Println(err)
		return
	}
	engine.ShowSQL(true)
	engine.SetTableMapper(core.SnakeMapper{})
	handler.InitDao(engine)
}

type str string
type s = string

func main() {
	router := routers.InitRouter()

	//router.Start(":8080")
	readTimeout := conf.Config.Server.ReadTimeout
	writeTimeout := conf.Config.Server.WriteTimeout
	endPoint := fmt.Sprintf(":%d", conf.Config.Server.Port)
	maxHeaderBytes := 1 << 20
	server := &http.Server{
		Addr:           endPoint,
		Handler:        router,
		ReadTimeout:    readTimeout,
		WriteTimeout:   writeTimeout,
		MaxHeaderBytes: maxHeaderBytes,
	}
	_ = server.ListenAndServe()
}
